/**
 * 
 */
package com.suyx.job.service.area.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import com.suyx.job.controller.MhxyHundrSmTaskController;
import com.suyx.job.entity.RoleInfo;
import com.suyx.job.entity.TuInfo;
import com.suyx.job.enums.SectEnums;
import com.suyx.job.utils.ImagePHash;
import com.suyx.job.utils.ImageUtils;
import com.suyx.job.utils.ScreenCapture;
import com.suyx.job.utils.SysStaticData;

import lombok.extern.slf4j.Slf4j;

/**
 * @author syx
 *
 */
@Slf4j
@Service
public class GoToSmAreaServiceImpl extends AbstractAreaService {

	@Override
	public boolean execute(String device, String path, String area) {
		try {
			String[] infos = SysStaticData.CA1.get("神木").split("&");
			userCA1(device, infos);

			// 屏蔽玩家
			ImagePHash p = new ImagePHash();
			ScreenCapture.snapshot(device,path, "screen.png");
	        ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
	                new FileOutputStream(path + device +".png"), 15,409,49,37);
	        String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
	        String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH +"chuangkou.png")));
	        int score =  p.distance(image1, image2);
	        if(score == 0){
	        	ScreenCapture.mouseClick(device, "411 491");//点击缩放
	        }
			ScreenCapture.mouseClick(device, "39 249");//
			isTanwei(device, path);
			
			// 点击NPC传送
			ScreenCapture.mouseClick(device, "716 219");//
			ScreenCapture.mouseClick(device, "1207 350");//
			
			//解除屏蔽
			ScreenCapture.mouseClick(device, "47 656");//
			//ScreenCapture.mouseClick(device, "39 249");//
			//ScreenCapture.mouseClick(device, "387 670");
			
			// 点击地图
			ScreenCapture.mouseClick(device, "148 38");//
			ScreenCapture.mouseClick(device, "568 498");//
			ScreenCapture.mouseClick(device, "1114 36");//
			Thread.sleep(20 * 1000L);
			// 到达神木林
			ScreenCapture.mouseClick(device, "272 207");//
			Thread.sleep(2 * 1000L);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean support(String area) {
		return SectEnums.SM.getCode().equals(area);
	}

	// 判断屏蔽之后是否隐藏了摊位
	private boolean isTanwei(String device, String path) throws Exception, IOException {
		ScreenCapture.snapshot(device, path, "screen.png");
		ImagePHash p = new ImagePHash();

		ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 33,
				453, 31, 56);
		String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
		String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "tanwei.png")));
		int score = p.distance(image1, image2);
		log.info("是否隐藏了摊位:"+score);
		if (score == 0) {
			// 去掉
			ScreenCapture.mouseClick(device, "27 437");//
			return true;
		}
		return false;
	}

	// 判断屏蔽之后是否隐藏了主页面
	private void isyingCang(String device, String path) throws Exception, IOException {
		ScreenCapture.snapshot(device, path, "screen.png");
		ImagePHash p = new ImagePHash();

		ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 26,
				545, 46, 40);// 隐藏
		String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
		String image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "yingcang.png")));
		int score = p.distance(image1, image2);
		log.info("是否隐藏了主页面:"+score);
		if (score == 0) {
			// 去掉
			ScreenCapture.mouseClick(device, "26 545");//
		}
		
		ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + ".png"), 33,
				453, 31, 56);
		image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
		image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "tanwei.png")));
		score = p.distance(image1, image2);
		log.info("是否隐藏了摊位:"+score);
		if (score == 0) {
			// 去掉
			ScreenCapture.mouseClick(device, "27 437");//
		}
	}

	@Override
	public boolean goToPoint(RoleInfo roleInfo, String path, TuInfo tuInfo) {
		String device = roleInfo.getDevice();
		String area = tuInfo.getZuobiaoX()+" "+tuInfo.getZuobiaoY();
		// TODO Auto-generated method stub
		return false;
	}

}
